<?php
/**
* Create On 2014-1-3 10:00:06
* Author: jiangyuchao
* E-mail: jiangyuchao@iwind-tech.com
*/

class feedbackexercise extends Model{
	
	/**
	 * 获取反馈题目列表（分页）
	 * @param unknown_type $page
	 * @param unknown_type $num
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getFbExListPageModel($keywords, $subId = 0,  $page = 1, $num = 8){
		$select = "SELECT `feedback_exercise`.*, `uuser`.`user_realname`, `uuser`.`user_id`, `subject`.`sub_cont`, `picture`.`pic_link` "
			 . "FROM `feedback_exercise` "
			 . "LEFT JOIN `uuser` ON `feedback_exercise`.`fe_user_id` = `uuser`.`user_id` "
			 . "LEFT JOIN `subject` ON `feedback_exercise`.`sub_id` = `subject`.`sub_id` "
			 . "LEFT JOIN `picture` ON `picture`.`pic_id` = `feedback_exercise`.`fe_pic_id` ";
		$filter = "WHERE 1 ";
		if ($subId){
			$filter .= "AND `feedback_exercise`.`sub_id` = '".$subId."' ";
		}
		if ($keywords){
			$filter .= "AND `feedback_exercise`.`fe_title` LIKE '%".$keywords."%' ";
		}
		$order = "ORDER BY `feedback_exercise`.`fe_create_time` DESC ";
		$limit = "LIMIT ".($page-1)*$num.",".$num."";
		$sql = $select.$filter.$order.$limit;
		$sqlTotal = $select.$filter.$order;
		//echo $sql;
		$list = $this->fetchAll($sql);
		$total = count($this->fetchAll($sqlTotal));
		$totalPage = ceil($total / $num);
		return array('page'=>$page,'list'=>$list,'total'=>$total,'totalPage'=>$totalPage);
	}
	
	/**
	 * 根据ID获取反馈题目详情
	 * @param unknown_type $fbExId
	 */
	public function getFbExDetailById($fbExId){
		$sql = "SELECT `feedback_exercise`.*, `uuser`.`user_realname`, `uuser`.`user_id`, `subject`.`sub_cont`, `picture`.`pic_link` "
			 . "FROM `feedback_exercise` "
			 . "LEFT JOIN `uuser` ON `feedback_exercise`.`fe_user_id` = `uuser`.`user_id` "
			 . "LEFT JOIN `subject` ON `feedback_exercise`.`sub_id` = `subject`.`sub_id` "
			 . "LEFT JOIN `picture` ON `picture`.`pic_id` = `feedback_exercise`.`fe_pic_id` "
			 . "WHERE `feedback_exercise`.`fe_id` = '".$fbExId."'";
		return $this->fetchRow($sql);
	}
	
	/**
	 * 根据Id删除反馈题目
	 * @param unknown_type $fbId
	 * @return resource
	 */
	public function delFeedback($fbId){
		$sql = "DELETE FROM `howdo`.`feedback_exercise` WHERE `feedback_exercise`.`fe_id` = '".$fbId."'";
		return $this->del($sql);
	}
}
